/*
 * RCEI VOICE
 *
 */

package es.ucm.disia.rcei.example.voice.test;

// demo.sphinx.hellodigits;

import java.io.File;
import java.io.IOException;
import java.net.URL;

import edu.cmu.sphinx.frontend.util.Microphone;
import edu.cmu.sphinx.recognizer.Recognizer;
import edu.cmu.sphinx.result.Result;
import edu.cmu.sphinx.util.props.ConfigurationManager;
import edu.cmu.sphinx.util.props.PropertyException;
import es.ucm.disia.rcei.RCEICommand;
import es.ucm.disia.rcei.RCEIMessageImpl;
import es.ucm.disia.rcei.RCEModel;
import es.ucm.disia.rcei.RCEModelImpl;

/**
 * A simple HelloDigits demo showing a simple speech application built using
 * Sphinx-4. This application uses the Sphinx-4 endpointer, which automatically
 * segments incoming audio into utterances and silences.
 */
public class Encrucijada_Voice extends Thread
{
	// Obtaining a log object (Common Logging uses our log4j properties file by default)
    //private Log logger = LogFactory.getLog(Capture.class);
    private long timeSleep;            
    private RCEModel rceModel;
    
    public Encrucijada_Voice (String rceFilename)
    {        
        try {
            rceModel = new RCEModelImpl(rceFilename);
            //two seconds
            timeSleep=2000;         
            rceModel.connect();
        } 
        catch (Exception e)
        {
            // Catch a more specific exception!!!
        }
    }
    
	/**
	 * Main method for running the HelloDigits demo.
	 */
	public void run() 
	{
		try 
		{
			URL url;
			/*if (args.length > 0) {
				url = new File(args[0]).toURI().toURL();
			} else {*/
				url = Encrucijada_Voice.class.getResource("hellodigits.config.xml");
			//}

			ConfigurationManager cm = new ConfigurationManager(url);

			Recognizer recognizer = (Recognizer) cm.lookup("recognizer");
			Microphone microphone = (Microphone) cm.lookup("microphone");

			/* allocate the resource necessary for the recognizer */
			recognizer.allocate();

			/* the microphone will keep recording until the program exits */
			if (microphone.startRecording()) {

				System.out
						.println("Say any digit(s): e.g. \"two oh oh four\", "
								+ "\"three six five\".");

				while (true) {
					System.out
							.println("Start speaking. Press Ctrl-C to quit.\n");

					/*
					 * This method will return when the end of speech is
					 * reached. Note that the endpointer will determine the end
					 * of speech.
					 */
					Result result = recognizer.recognize();
					
					if (result != null) 
					{
						String resultText = result.getBestResultNoFiller();						
						
						RCEICommand cmd=generateCommandText(resultText);//null;//new RCEICommand("agent"+resultText,"speak");											
						RCEICommand cmd2=generateCommandAction(resultText);
						//cmd.setDirComp("agent "+resultText +" waiting your orders");
						
						System.out.println("You said: " + resultText + "\n");
						
						if (cmd!=null)
						{
							RCEIMessageImpl msg=new RCEIMessageImpl();
							msg.addInstruction(cmd);
							if (cmd2!=null)
							msg.addInstruction(cmd2);
							rceModel.send(msg);							
						}
						
						//System.out.println("You said: " + resultText + "\n");
						//this.sleep(3000);
					} 
					else 
					{
						System.out.println("I can't hear what you said.\n");
					}
				}
			} else {
				System.out.println("Cannot start microphone.");
				recognizer.deallocate();
				System.exit(1);
			}
		} catch (IOException e) {
			System.err.println("Problem when loading HelloDigits: " + e);
			e.printStackTrace();
		} catch (PropertyException e) {
			System.err.println("Problem configuring HelloDigits: " + e);
			e.printStackTrace();
		} catch (InstantiationException e) {
			System.err.println("Problem creating HelloDigits: " + e);
			e.printStackTrace();
		} /*catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
	}
	
	private RCEICommand generateCommandText(String resultText)
	{
		RCEICommand cmd=null;//new RCEICommand("agent"+resultText,"speak");
		
		String phrase="none";
		String subject="beggar";
		//Cases
		if (resultText.equalsIgnoreCase("one"))
		{
			cmd=new RCEICommand(subject,"speak");
			cmd.setDirComp("one");
		}
		else if (resultText.equalsIgnoreCase("two"))
		{
			cmd=new RCEICommand(subject,"speak");
			cmd.setDirComp("two");												
		}
		else if (resultText.equalsIgnoreCase("three"))
		{
			cmd=new RCEICommand(subject,"speak");
			cmd.setDirComp("three");												
		}
		else if (resultText.equalsIgnoreCase("four"))
		{
			cmd=new RCEICommand(subject,"speak");
			cmd.setDirComp("four");												
		}
		else if (resultText.equalsIgnoreCase("four"))
		{
			cmd=new RCEICommand(subject,"speak");
			cmd.setDirComp("agent4 presente Dios mio no siento las piernas");												
		}
		else if (resultText.equalsIgnoreCase("five"))
		{
			cmd=new RCEICommand(subject,"speak");
			cmd.setDirComp("five");												
		}
		else if (resultText.equalsIgnoreCase("six"))
		{
			cmd=new RCEICommand(subject,"speak");
			cmd.setDirComp("six");												
		}
		else if (resultText.equalsIgnoreCase("seven"))
		{
			cmd=new RCEICommand(subject,"speak");
			cmd.setDirComp("seven");												
		}
		else if (resultText.equalsIgnoreCase("eight"))
		{
			cmd=new RCEICommand(subject,"speak");
			cmd.setDirComp("eight");												
		}
		else if (resultText.equalsIgnoreCase("nine"))
		{
			cmd=new RCEICommand(subject,"speak");
			cmd.setDirComp("nine");												
		}
		
		return cmd;
	}
	
	private RCEICommand generateCommandAction(String resultText)
	{
		RCEICommand cmd=null;//new RCEICommand("agent"+resultText,"speak");
		
		String phrase="none";
		
		String subject="beggar";
		//Cases
		//Ir hacia la anciana
		if (resultText.equalsIgnoreCase("one"))
		{
			cmd=new RCEICommand(subject,"go");
			cmd.setDirComp("oldwoman");
		}
		//
		else if (resultText.equalsIgnoreCase("two"))
		{
			cmd=new RCEICommand(subject,"speak");
			cmd.setDirComp("hello");												
		}
		else if (resultText.equalsIgnoreCase("three"))
		{
			cmd=new RCEICommand(subject,"attack");
			cmd.setDirComp("oldwoman");												
		}
		else if (resultText.equalsIgnoreCase("four"))
		{
			cmd=new RCEICommand(subject,"go");
			cmd.setDirComp("wagon1");												
		}
		else if (resultText.equalsIgnoreCase("five"))
		{
			cmd=new RCEICommand("environment","create");
			cmd.setDirComp("woman_fata");
			//cmd.setModeComp("26,10,30");
			cmd.setPlaceComp("cime_reuel1");
		}
		else if (resultText.equalsIgnoreCase("six"))
		{
			cmd=new RCEICommand(subject,"go");
			cmd.setDirComp("wagon2");									
		}
		else if (resultText.equalsIgnoreCase("seven"))
		{
			cmd=new RCEICommand("environment","change");
			cmd.setDirComp("weather");				
			cmd.setModeComp("2");
			cmd.setPlaceComp("cime_reuel1");			
		}
		else if (resultText.equalsIgnoreCase("eight"))
		{
			cmd=new RCEICommand(subject,"go");
			cmd.setDirComp("doorHouse1Enter");												
		}
		else if (resultText.equalsIgnoreCase("nine"))
		{
			cmd=new RCEICommand(subject,"go");
			cmd.setDirComp("pedestal");												
		}
		
		return cmd;
	}
	
	public static void main(String args[]) 
	{
        System.out.println("RCEI VOICE!...");
        //System.out.println("rce" + File.separator + "nwn1.rcei");
        Encrucijada_Voice hd = new Encrucijada_Voice("rce" + File.separator + "nwn1.rcei"); 
        // It should be: examples\pig\rce\nwn1.rcei
        hd.run();        
    }
}
